package com.study.happystudy.controller;

import com.study.happystudy.entity.ResultEntity;
import com.study.happystudy.po.*;
import com.study.happystudy.service.impl.MessageServiceImpl;
import com.study.happystudy.service.impl.UserServiceImpl;
import com.study.happystudy.tool.ColorChange;
import jakarta.annotation.Resource;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("message/")
public class MessageController {

    @Resource
    MessageServiceImpl messageService;
    @Resource
    UserServiceImpl userService;

    /**
     * 发送私信
     *
     * @param senderId       发信人ID
     * @param receiverId     收信人ID
     * @param messageContent 消息内容
     * @return JSON
     */
    @PostMapping("sendPrivateMessage")
    public String sendPrivateMessage(
            @RequestParam("senderId") int senderId,
            @RequestParam("receiverId") int receiverId,
            @RequestParam("messageContent") String messageContent) {
        int result = messageService.sendPrivateMessage(senderId, receiverId, messageContent);
        if (result > 0) {
            return ResultEntity.Success(result, "发送私信成功");
        } else {
            return ResultEntity.Fail(result, "发送私信失败");
        }
    }

    /**
     * 撤回私信
     *
     * @param messageId 消息ID
     * @return JSON
     */
    @PostMapping("revokePrivateMessage")
    public String revokePrivateMessage(@RequestParam("messageId") int messageId) {
        int result = messageService.revokePrivateMessage(messageId);
        if (result > 0) {
            return ResultEntity.Success();
        } else {
            return ResultEntity.Fail(result, "撤回私信失败");
        }
    }

    /**
     * 根据ID获取消息
     *
     * @param messageId 消息ID
     * @return JSON
     */
    @GetMapping("getMessageById")
    public Message getMessageById(@RequestParam("messageId") int messageId) {
        return messageService.getMessageById(messageId);
    }

    /**
     * 根据ID删除消息
     *
     * @param messageId 消息ID
     * @return JSON
     */
    @PostMapping("deleteMessageById")
    public String deleteMessageById(@RequestParam("messageId") int messageId) {
        int result = messageService.deleteMessageById(messageId);
        if (result > 0) {
            return ResultEntity.Success(1, "操作成功");
        } else {
            return ResultEntity.Fail(result, "删除消息失败");
        }
    }

    /**
     * 发送系统消息
     *
     * @param receiverId     收信人ID
     * @param messageContent 消息内容
     * @return JSON
     */
    @PostMapping("sendSystemMessage")
    public String sendSystemMessage(
            @RequestParam("receiverId") int receiverId,
            @RequestParam("messageContent") String messageContent) {
        int result = messageService.sendSystemMessage(0, receiverId, messageContent);
        if (result > 0) {
            return ResultEntity.Success(1, "发送系统消息成功");
        } else {
            return ResultEntity.Fail(result, "发送系统消息失败");
        }
    }

    /**
     * 发送课程消息
     *
     * @param courseId       课程ID
     * @param receiverId     收信人ID
     * @param messageContent 消息内容
     * @return JSON
     */
    @PostMapping("sendCourseMessage")
    public String sendCourseMessage(
            @RequestParam("courseId") int courseId,
            @RequestParam("receiverId") int receiverId,
            @RequestParam("messageContent") String messageContent) {
        int result = messageService.sendCourseMessage(courseId, receiverId, messageContent);
        if (result > 0) {
            return ResultEntity.Success(1, "发送课程消息成功");
        } else {
            return ResultEntity.Fail(result, "发送课程消息失败");
        }
    }

    /**
     * 发送活动消息
     *
     * @param activityId     发信人ID
     * @param receiverId     收信人ID
     * @param messageContent 消息内容
     * @return JSON
     */
    @PostMapping("sendActivityMessage")
    public String sendEventMessage(
            @RequestParam("activityId") int activityId,
            @RequestParam("receiverId") int receiverId,
            @RequestParam("messageContent") String messageContent) {
        int result = messageService.sendActivityMessage(activityId, receiverId, messageContent);
        if (result > 0) {
            return ResultEntity.Success(1, "发送活动消息成功");
        } else {
            return ResultEntity.Fail(result, "发送活动消息失败");
        }
    }

    /**
     * 发送社区消息
     *
     * @param senderId       发信人ID
     * @param receiverId     收信人ID
     * @param messageContent 消息内容
     * @return JSON
     */
    @PostMapping("sendCommunityMessage")
    public String sendCommunityMessage(
            @RequestParam("senderId") int senderId,
            @RequestParam("receiverId") int receiverId,
            @RequestParam("messageContent") String messageContent) {
        int result = messageService.sendCommunityMessage(senderId, receiverId, messageContent);
        if (result > 0) {
            return ResultEntity.Success(result);
        } else {
            return ResultEntity.Fail(result, "发送社区消息失败");
        }
    }

    /**
     * 根据关键词查找消息
     *
     * @param userId  用户ID
     * @param keyword 关键词
     * @return JSON
     */
    @GetMapping("searchMessagesByKeyword")
    public List<Message> searchMessagesByKeyword(
            @RequestParam("userId") int userId,
            @RequestParam("keyword") String keyword) {
        return messageService.searchMessagesByKeyword(userId, keyword);
    }

    @GetMapping("getMessagesByUserId")
    public List<Message> getMessagesByUserId(@RequestParam("userId") int userId, @RequestParam("messageType") String messageType) {
        return messageService.getMessagesByUserId(userId, messageType);
    }

    @GetMapping("getPrivateMessage")
    public List<Message> getPrivateMessage(@RequestParam int userId1, @RequestParam int userId2) {
        try {
            return messageService.getPrivateMessage(userId1, userId2);
        } catch (Exception e) {
            ColorChange.Red(e.getMessage() + "\n");
            return null;
        }
    }

    @GetMapping("getPrivateMessageList")
    public List<PrivateMessageList> getPrivateMessageList(@RequestParam("userId") int userId) {
        List<Integer> l = messageService.getFriendsId(userId);
        List<PrivateMessageList> res = new ArrayList<>();
        for (Integer integer : l) {
            if (integer == userId) {
                continue;
            }
            PrivateMessageList privateMessageList = messageService.getPrivateMessageList(userId, integer);
            if (privateMessageList == null) {
                continue;
            }
            res.add(privateMessageList);
        }

//        按照date排序
        res.sort((o1, o2) -> o2.getDate().compareTo(o1.getDate()));

        return res;
    }

    @GetMapping("getMessageUnreadCount")
    public UnreadCount getSystemMessageUnreadCount(@RequestParam("userId") int userId) {
        UnreadCount res = new UnreadCount();
        res.setCourseMessageUnreadCount(messageService.getCourseMessageUnreadCount(userId));
        res.setCommunityMessageUnreadCount(messageService.getCommunityMessageUnreadCount(userId));
        res.setActivityMessageUnreadCount(messageService.getActivityMessageUnreadCount(userId));
        res.setSystemMessageUnreadCount(messageService.getSystemMessageUnreadCount(userId));
        return res;
    }

    @PostMapping("privateReadAll")
    public String privateReadAll(@RequestParam("userId1") int userId1, @RequestParam("userId2") int userId2) {
        try {
            messageService.privateReadAll(userId1, userId2);
        } catch (Exception e) {
            return ResultEntity.Fail(0, "私信未读数清零失败");
        }
        return ResultEntity.Success(1, "成功");
    }

    @PostMapping("systemReadAll")
    public String systemReadAll(@RequestParam("userId") int userId) {
        try {
            messageService.systemReadAll(userId);
            return ResultEntity.Success(1, "成功");
        } catch (Exception e) {
            return ResultEntity.Fail(0, "系统未读数清零失败");
        }
    }

    @PostMapping("activityReadAll")
    public String activityReadAll(@RequestParam("userId") int userId) {
        try {
            messageService.activityReadAll(userId);
            return ResultEntity.Success(1, "成功");
        } catch (Exception e) {
            return ResultEntity.Fail(0, "活动未读数清零失败");
        }
    }

    @PostMapping("courseReadAll")
    public String courseReadAll(@RequestParam("userId") int userId) {
        try {
            messageService.courseReadAll(userId);
        } catch (Exception e) {
            return ResultEntity.Fail(0, "课程未读数清零失败");
        }
        return ResultEntity.Success(1, "成功");
    }

    @PostMapping("communityReadAll")
    public String communityReadAll(@RequestParam("userId") int userId) {
        try{
            messageService.communityReadAll(userId);
            return ResultEntity.Success(1, "成功");
        }catch (Exception e){
            return ResultEntity.Fail(0,"社区未读数清零失败");
        }
    }

    @GetMapping("getPiig")
    public Pooh getPiig(){
        return new Pooh(List.of(List.of(5.33,0,16777215,"周杰伦","老师好!"),
                List.of(4.13,0,16777215,"Pooh","今天上什么课？"),
                List.of(8.33,0,16777215,"我是好学生","老师好"),
                List.of(47.33,0,16777215,"小饭团","开始困了"),
                List.of(99.73,0,16777215,"Pooh","C?")));
    }

}
